package com.zzyl.framework.interceptor;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.zzyl.common.core.domain.model.LoginUser;
import com.zzyl.common.utils.SecurityUtils;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

// java example
@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {


    @Override
    public void insertFill(MetaObject metaObject) {
        this.setFieldValByName("createTime",new Date(),metaObject);
        this.setFieldValByName("createBy",String.valueOf(getLoginUser()),metaObject);

    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.setFieldValByName("updateTime",new Date(),metaObject);
        this.setFieldValByName("updateBy",String.valueOf(getLoginUser()) ,metaObject);
    }

    public Long getLoginUser(){
        LoginUser loginUser = SecurityUtils.getLoginUser();
        //判断获取的登录用户是否为null，为空则返回-1  不为空则将用户的id返回
        if (loginUser !=null){
            return loginUser.getUserId();
        }
        return -1l;
    }
}